package com.study.talk.ui.app.home.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.study.talk.ui.adapter.ViewPager2StateAdapter
import com.study.talk.ui.app.base.BaseFragment
import com.study.talk.databinding.FragmentContactsBinding

class ContactsFragment : BaseFragment() {
    private lateinit var binding: FragmentContactsBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentContactsBinding.inflate(inflater, container, false)
        initDate()
        return binding.root
    }

    private fun initDate() {
        val fragmentList = listOf(FriendFragment(), GroupFragment())
        binding.contactsTab.addTab(binding.contactsTab.newTab())
        binding.contactsTab.addTab(binding.contactsTab.newTab())
        val titles = listOf("好友", "群聊")
        val adapter = ViewPager2StateAdapter(requireActivity(), fragmentList)
        binding.contactsViewpage.adapter = adapter
        val mediator = TabLayoutMediator(
            binding.contactsTab, binding.contactsViewpage
        ) { tab: TabLayout.Tab, position: Int ->
            tab.setText(
                titles[position]
            )
        }
        mediator.attach()
    }
}